package com.igroupes.ovineherd.model.attribute.file;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.FileStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.utils.ListUtils;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 枚举
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
@Component
public class FileModelData extends FileBasicAttribute {


    @Value("${ovhapi.file.url}")
    private String fileUrl;

    private static String url;
    @PostConstruct
    public void  into(){
        url = fileUrl;
    }
    public FileModelData() {
        super();
    }


    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        FileModelData fileModelData = (FileModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        return super.getAttribute(modelDataJsonAttributeQuery);
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        List<FieldApiResponse> fieldApiResponses = super.verifyRequestData(modelAttributeEntity, value);
        if(value == null){
            return fieldApiResponses;
        }
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity ) {
        StringBuilder checkSuffixList = new StringBuilder(Convert.toStr(dataModelAttributeEntity.getJson().get("checkSuffix")));
        if(ObjectUtil.isNotNull(dataModelAttributeEntity.getJson().get("suffix"))){
            List<String> suffix = ListUtils.parseString(dataModelAttributeEntity.getJson().get("suffix").toString());
            for (String str :
                    suffix) {
                checkSuffixList.append(",").append(str);
            }
        }
        return new JSONObject( new FileStyle(
                dataModelAttributeEntity.getName(),
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
                "",
                Convert.toInt(dataModelAttributeEntity.getJson().get("size")),
                checkSuffixList.toString(), url));
    }
}